# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import datetime
import sys

sys.path.extend(["F:/ML/BNC", "D:/BNC", "/www/wwwroot/BNC"])


from WebServer.blog.DB.DBValueConstants import *
from DataBase.MysqlObject import *
from sqlalchemy import Column, String, Integer, Date, ForeignKey, UniqueConstraint
from sqlalchemy.ext.declarative import declarative_base

# DB_URI = 'mysql+pymysql://{username}:{password}@{host}:{port}/{db}?charset=utf8'.format(
#     **{'username': 'root', 'password': MYSQLServerPW_H_Local, 'host': "127.0.0.1", 'port': '3306',
#        'db': "stock_tag_manager"})
DB_URI = 'mysql+pymysql://{username}:{password}@{host}:{port}/{db}?charset=utf8'.format(
    **{'username': 'root', 'password': MYSQLServerPW_H_Local, 'host': 'localhost', 'port': '3306',
       'db': "stock_tag_manager"})
engine = create_engine(DB_URI, echo=False)
DBSession_mysql = sessionmaker(bind=engine, )
Base = declarative_base(engine)

HDY_DB_URI = 'mysql+pymysql://{username}:{password}@{host}:{port}/{db}?charset=utf8'.format(
    **{'username': 'root', 'password': MYSQLServerPW_H_Local, 'host': 'localhost', 'port': '3306',
       'db': "stock_tag_manager"})
# HDY_DB_URI = 'mysql+pymysql://{username}:{password}@{host}:{port}/{db}?charset=utf8'.format(
#     **{'username': 'root', 'password': "root", 'host': MySQLServerIP_199, 'port': '3306',
#        'db': ForumPosts_LMysqlDB})
HDY_engine = create_engine(HDY_DB_URI, echo=False)
HDY_DBSession_mysql = sessionmaker(bind=HDY_engine, )
HDYBase = declarative_base(engine)


# 互动易的数据
class HuDongYiQuestion(HDYBase):
    # 表的名字:
    __tablename__ = HuDongYiQuestionTableName

    # 表的结构:
    code = Column(String(10), primary_key=True)  #
    name = Column(String(10))  #
    time = Column(String(20))  #
    question = Column(String(400))  #
    answer = Column(String(1000))  #
    post_url = Column(String(400))  #
    timestamp = Column(Integer, primary_key=True)
    type = Column(Integer)  # 多个数据源，需要有一个类别参数来区分 全景网类型是1


class TagStatus(Base):
    # 表的名字:
    __tablename__ = 'tag_status'
    __table_args__ = {"useexisting": True}

    # 表的结构:
    tag = Column(String(255), primary_key=True)
    enable = Column(Integer, default=DISABLE)
    storage_date = Column(Date, default=datetime.date.today())  # 开盘价


class TagRename(Base):
    # 表的名字:
    __tablename__ = 'tag_rename'
    __table_args__ = (
        UniqueConstraint('origin', 'to', ),
    )
    # 表的结构:
    id = Column(Integer, primary_key=True, autoincrement=True)
    origin = Column(String(255), unique=True)
    to = Column(String(255), ForeignKey("tag_status.tag"))
    source = Column(String(3000), nullable=True)
    enable = Column(Integer, default=DISABLE)
    fresh = Column(Integer, default=FRESH)
    storage_date = Column(Date, default=datetime.date.today())

    # def __getstate__(self):
    #     state = {}
    #     for k, v in self.__dict__.items():
    #         if k != '_sa_instance_state':
    # if not k.startswith("_"):
    #     state[k] = v
    # return state
    #
    # def __setstate__(self, d):
    #     self.__dict__ = d


class CodeTagDate(Base):
    __tablename__ = 'code_tag_date'
    __table_args__ = (
        UniqueConstraint('code', 'tag', 'date', 'source', ),
    )

    id = Column(Integer, primary_key=True, autoincrement=True)
    code = Column(String(255))
    name = Column(String(255), default='')
    tag = Column(String(255), ForeignKey('tag_rename.origin'))
    date = Column(Date)
    source = Column(String(3000), nullable=True)
    enable = Column(Integer, default=ENABLE)
    fresh = Column(Integer, default=FRESH)
    storage_date = Column(Date, default=datetime.date.today())
    reason = Column(String(3000), nullable=True, default="")
    link = Column(String(3000), nullable=True, default="")

    # tag_to = relationship("TagRename", backref="uuu")

    # def __getstate__(self):
    #     state = {}
    #     for k, v in self.__dict__.items():
    #         if k != '_sa_instance_state':
    #         if not k.startswith("_"):
    #             state[k] = v
    #     return state
    #
    # def __setstate__(self, d):
    #     self.__dict__ = d
    #
    # class Meta:
    #     unique_together = ('code', 'tag', 'date', 'source')

    def __str__(self):
        return "code_tag_date object , id :{},code:{},name:{},tag:{}".format(self.id, self.code, self.name, self.tag)


class CodeTagDateWeightTemp(Base):
    __tablename__ = 'code_tag_date_weight_temp'
    __table_args__ = (
        UniqueConstraint('code', 'tag', 'cur_date', 'cal_date', ),
    )
    id = Column(Integer, primary_key=True, autoincrement=True)
    code = Column(String(255))
    tag = Column(String(255))
    cur_up_times = Column(Integer, default=0)
    cur_date = Column(Integer, nullable=False)
    cur_count = Column(Integer, default=0)
    cal_date = Column(Integer, nullable=False)


class CodeTagRelation(Base):
    __tablename__ = 'code_tag_relation'
    __table_args__ = (
        UniqueConstraint('code', 'tag', ),
    )
    id = Column(Integer, primary_key=True, autoincrement=True)
    code = Column(String(255))
    tag = Column(String(255))
    storage_date = Column(Date, nullable=False, default=datetime.date.today())
    start_date = Column(Date, nullable=False, default=datetime.date.today())
    end_date = Column(Date, default=datetime.date.max, nullable=False)
    enable = Column(Integer, default=DISABLE)
    reason = Column(String(3000), nullable=True, default="")
    link = Column(String(3000), nullable=True, default="")
    source = Column(String(3000), nullable=True)
    del_reason = Column(String(1000), nullable=True, default="")


Base.metadata.create_all()

# class CodeTagRelation(models.Model):
#     id = models.AutoField(primary_key=True)
#     code = models.CharField(max_length=255)
#     tag = models.CharField(max_length=255)
#     start_date = models.IntegerField(blank=False)
#     end_date = models.IntegerField(default=9999999, blank=True)
#     enable = models.IntegerField(default=-1)
#     objects = models.Manager()
#
#     class Meta:
#         db_table = 'code_tag_relation'
#         unique_together = ('code', 'tag',)
#         ordering = ['-enable']
#
#
# class CodeTagDateWeightTemp(models.Model):
#     id = models.AutoField(primary_key=True)
#     code = models.CharField(max_length=255)
#     tag = models.CharField(max_length=255)
#     cur_up_times = models.IntegerField(default=0)
#     cur_date = models.IntegerField(blank=False)
#     cur_count = models.IntegerField(default=0)
#     cal_date = models.IntegerField(blank=False)
#     objects = models.Manager()
#
#     class Meta:
#         db_table = 'code_tag_date_weight_temp'
#         unique_together = ('code', 'tag', 'cur_date', 'cal_date')
#

# class TagStatus(models.Model):
#     tag = models.CharField(max_length=255, primary_key=True)
#     enable = models.IntegerField(default=-1)
#     storage_date = models.DateField(default=datetime.date.today())
#     objects = models.Manager()
#
#     class Meta:
#         db_table = 'tag_status'
#         ordering = ['-enable', '-storage_date']
#
#
# class TagRename(models.Model):
#     id = models.AutoField(primary_key=True)
#     origin = models.CharField(max_length=255, unique=True)
#     to = models.ForeignKey(to='TagStatus', on_delete=models.DO_NOTHING, to_field='tag', related_name='to_tag',
#                            related_query_name='to_tag')
#     source = models.CharField(max_length=255, blank=True)
#     enable = models.IntegerField(default=-1)
#     fresh = models.IntegerField(default=1)
#     storage_date = models.DateField(default=datetime.date.today())
#     objects = models.Manager()
#
#     class Meta:
#         db_table = 'tag_rename'
#         unique_together = [('origin', 'to',)]
#
#         ordering = ['-enable', '-storage_date']
#
#
# class CodeTagDate(models.Model):
#     id = models.AutoField(primary_key=True)
#     code = models.CharField(max_length=255)
#     name = models.CharField(max_length=255, blank=True)
#     tag = models.ForeignKey(to='TagRename', on_delete=models.DO_NOTHING, to_field='origin')
#     tag = models.CharField(max_length=255)
# date = models.DateField()
# source = models.CharField(max_length=255, blank=True)
# enable = models.IntegerField(default=1)
# storage_date = models.DateField(default=datetime.date.today())
#
# class Meta:
#     db_table = 'code_tag_date'
#     unique_together = ('code', 'tag', 'date', 'source')
#     ordering = ['-enable', '-storage_date']
#
# def __str__(self):
#     return "code_tag_date object , id :{},code:{},name:{},tag:{}".format(self.id, self.code, self.name, self.tag)
#
#
# class CodeTagRelation(models.Model):
#     id = models.AutoField(primary_key=True)
#     code = models.CharField(max_length=255)
#     tag = models.CharField(max_length=255)
#     start_date = models.IntegerField(blank=False)
#     end_date = models.IntegerField(default=9999999, blank=True)
#     enable = models.IntegerField(default=-1)
#     objects = models.Manager()
#
#     class Meta:
#         db_table = 'code_tag_relation'
#         unique_together = ('code', 'tag',)
#         ordering = ['-enable']
#
#
# class CodeTagDateWeightTemp(models.Model):
#     id = models.AutoField(primary_key=True)
#     code = models.CharField(max_length=255)
#     tag = models.CharField(max_length=255)
#     cur_up_times = models.IntegerField(default=0)
#     cur_date = models.IntegerField(blank=False)
#     cur_count = models.IntegerField(default=0)
#     cal_date = models.IntegerField(blank=False)
#     objects = models.Manager()
#
#     class Meta:
#         db_table = 'code_tag_date_weight_temp'
#         unique_together = ('code', 'tag', 'cur_date', 'cal_date')
#
